|
|
|
|
|
|
|
is truly irrelevant or you just need to move on and debug other areas of your code while in design time, use the Resume Next statement. Resume Next does not require any special object-oriented treatment, so it won't be discussed further unless it's truly warranted. |
|
|
|
|
|
|
|
|
The Visual Basic Books Online set includes some simple error-handling code for dealing with division-by-zero errors and other math errors. Let's create a simple class called ErrorHandler that has a method that encapsulates this logic. Start a new Visual Basic project. Using the Class Builder utility, create a class named ErrorHandler. You can also simply add a class module. Now, create a method named handleErrors. Its only argument is argErrorObj As Object (or As ErrObject if your situation requires early binding). It accepts the error object. |
|
|
|
|
|
|
|
|
Using the default startup Form1, create a class, FormController, that controls Form1. Declare a private module variable, the Form As New Form1. Create a method, showForm. Add a code module, modMain. The code for FormController class is shown in Listing 18.1. The ErrorHandler class code is shown in Listing 18.2. ErrorHandler is declared globally as |
|
|
|
|
|
|
|
|
Global As theErrorHandler As New ErrorHandler |
|
|
|
|
|
|
|
|
LISTING 18.1. THEshowFormMETHOD OF THEFormControllerCLASS. |
|
|
|
 |
|
|
|
|
1: Public Sub showForm
2: Dim iSomeNumber As Integer
3: Dim sglAnotherNumber As Single
4: Dim iResult As Integer
5: Dim sString As String
6:
7: On Error GoTo showFormErr
8: sglAnotherNumber = 51000.25
9: iResult = sglAnotherNumber / iSomeNumber
10: Exit Sub
11: showFormErr:
12: End Sub |
|
|
|
|
|
|
|
|
LISTING 18.2. THEhandleErrorsMETHOD OF THEErrorHandlerCLASS. IT IS ACCOMPANIED BY A HELPER METHOD,handleMathErrors. |
|
|
|
 |
|
|
|
|
1: Public Sub handleErrors(argErrorObj As Object)
2: handleMathErrors argErrorObj
3: End Sub
4: Private Sub handleMathErrors(argErrorObj As Object)
5: sMsg As String
|
|
|
|
|
|